import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

extension ImgPath on String {
  String get imgPath {
    return 'assets/images/' + this;
  }

  String get imgPathHome {
    return 'assets/images/home/' + this;
  }

  String get imgPathTabbar {
    return 'assets/images/tabbar/' + this;
  }

  String get imgPathMe {
    return 'assets/images/me/' + this;
  }

  String get imgPathTask {
    return 'assets/images/task/' + this;
  }

  String get imgPathMsg {
    return 'assets/images/msg/' + this;
  }

  String get imgPathLogin {
    return 'assets/images/login/' + this;
  }
}

ImageProvider loadImageProvider(
    {String? url, String? placeholder, double scale = 1.0}) {
  if (url == null || url.isEmpty) {
    return ExactAssetImage(
      placeholder ?? 'default_pic.png'.imgPathHome,
      scale: scale,
    );
  }
  if (url.startsWith('http')) {
    return CachedNetworkImageProvider(url, scale: scale);
  } else {
    return ExactAssetImage(url, scale: scale);
  }
}

Widget loadImage(
    {String? url,
    String? placeholder,
    double? width,
    double? height,
    Color? color,
    BoxFit fit = BoxFit.cover}) {
  if (url == null || url.isEmpty) {
    return Image.asset(
      placeholder ?? 'default_pic.png'.imgPathHome,
      width: width,
      height: height,
      fit: fit,
    );
  }
  if (url.startsWith('http')) {
    return CachedNetworkImage(
      imageUrl: url,
      width: width,
      height: height,
      fit: fit,
      progressIndicatorBuilder: (context, url, progress) {
        return CupertinoActivityIndicator(
          radius: 15.r,
        );
      },
      errorWidget: (context, str, obj) {
        return loadImage(url: placeholder);
      },
    );
  } else {
    return Image.asset(
      url,
      width: width,
      height: height,
      color: color,
      fit: fit,
    );
  }
}

Widget loadAvater(
    {String? url,
    String? placeholder,
    required double size,
    BoxFit fit = BoxFit.cover}) {
  return ClipOval(
    child: loadImage(
        url: url,
        placeholder: 'avatar_default.png'.imgPathMe,
        width: size,
        height: size,
        fit: fit),
  );
}
